package db.dbTables;

import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import core.objects.BookLoan;
import core.objects.ObjectWithID;
import core.objects.enums.LoanStatusEnum;
import java.sql.SQLException;
import java.sql.Timestamp;

public class LoanTable extends AbstractDBTable {
    
    public static final String COLUMN_ID = "loan_id";
    public static final String COLUMN_BOOK_ID = "book_id";
    public static final String COLUMN_LOANER_ID = "loaner_id";
    public static final String COLUMN_LOANED_ID = "loaned_id";
    public static final String COLUMN_START_DATE = "start_date";
    public static final String COLUMN_END_DATE = "end_date";
    public static final String COLUMN_LOAN_STATUS = "loan_status";
    
    final public DbColumn id = new DbColumn(COLUMN_ID, Types.INTEGER, 11, false, this);
    final public DbColumn bookId = new DbColumn(COLUMN_BOOK_ID, Types.INTEGER, 11, false, this);
    final public DbColumn loanerId = new DbColumn(COLUMN_LOANER_ID, Types.INTEGER, 11, false, this);
    final public DbColumn loanedId = new DbColumn(COLUMN_LOANED_ID, Types.INTEGER, 11, false, this);
    final public DbColumn startDate = new DbColumn(COLUMN_START_DATE, Types.TIMESTAMP, -1, false, this);
    final public DbColumn endDate = new DbColumn(COLUMN_END_DATE, Types.TIMESTAMP, -1, true, this);
    final public DbColumn loanStatus = new DbColumn(COLUMN_LOAN_STATUS, Types.OTHER, -1, false, this);


    public LoanTable() {
        super("loan");
        setIDColumn(id);
    }
    
    @Override
	public List<DbColumn> getDbColumns(){
        List<DbColumn> list = new ArrayList<DbColumn>();
        list.add(id);
        list.add(bookId);
        list.add(loanerId);
        list.add(loanedId);
        list.add(startDate);
        list.add(endDate);
        list.add(loanStatus);
        return list;
    }

    @Override
    public ObjectWithID convertSqlTableToBaseObject(ResultSet resultSet) throws SQLException {
        int id = resultSet.getInt(COLUMN_ID);
        int bookId = resultSet.getInt(COLUMN_BOOK_ID);
        int loanerId = resultSet.getInt(COLUMN_LOANER_ID);
        int loanedToId = resultSet.getInt(COLUMN_LOANED_ID);
        Timestamp startTime = resultSet.getTimestamp(COLUMN_START_DATE);
        Timestamp endTime = resultSet.getTimestamp(COLUMN_END_DATE);
        LoanStatusEnum status = LoanStatusEnum.valueOfString(resultSet.getString(COLUMN_LOAN_STATUS));

        return new BookLoan(id, bookId, loanerId, loanedToId, startTime, endTime, status);
    }
    
}
